<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>视频上传测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 10px;
            border-radius: 4px;
        }
        .success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
    </style>
</head>
<body>
    <h1>视频上传测试</h1>
    
    <div class="form-group">
        <label for="title">视频标题:</label>
        <input type="text" id="title" value="测试视频" required>
    </div>
    
    <div class="form-group">
        <label for="description">视频描述:</label>
        <textarea id="description" rows="3">这是一个测试视频</textarea>
    </div>
    
    <div class="form-group">
        <label for="videoFile">视频文件:</label>
        <input type="file" id="videoFile" accept="video/*" required>
    </div>
    
    <div class="form-group">
        <label for="thumbnailFile">缩略图文件:</label>
        <input type="file" id="thumbnailFile" accept="image/*">
    </div>
    
    <div class="form-group">
        <label for="userId">用户ID:</label>
        <input type="number" id="userId" value="1" required>
    </div>
    
    <button onclick="uploadVideo()">上传视频</button>
    
    <div id="result"></div>
    
    <script>
        async function uploadVideo() {
            const title = document.getElementById('title').value;
            const description = document.getElementById('description').value;
            const videoFile = document.getElementById('videoFile').files[0];
            const thumbnailFile = document.getElementById('thumbnailFile').files[0];
            const userId = document.getElementById('userId').value;
            
            if (!title || !videoFile || !userId) {
                showResult('请填写所有必填字段', 'error');
                return;
            }
            
            try {
                const formData = new FormData();
                formData.append('title', title);
                formData.append('description', description);
                formData.append('video', videoFile);
                formData.append('userId', userId);
                
                if (thumbnailFile) {
                    formData.append('thumbnail', thumbnailFile);
                } else {
                    // 创建一个默认的缩略图文件
                    const defaultThumbnail = new File([''], 'default.jpg', { type: 'image/jpeg' });
                    formData.append('thumbnail', defaultThumbnail);
                }
                
                const response = await fetch('http://localhost:8080/api/videos/upload', {
                    method: 'POST',
                    body: formData
                });
                
                const result = await response.json();
                
                if (result.success) {
                    showResult('视频上传成功！<br>视频ID: ' + result.data.id + '<br>视频URL: ' + result.data.videoUrl, 'success');
                } else {
                    showResult('上传失败: ' + result.message, 'error');
                }
            } catch (error) {
                showResult('错误: ' + error.message, 'error');
            }
        }
        
        function showResult(message, type) {
            const resultDiv = document.getElementById('result');
            resultDiv.innerHTML = message;
            resultDiv.className = 'result ' + type;
        }
    </script>
</body>
</html> 